//
//  CircleView.swift
//  SmartPenSwift
//
//  Created by tql on 2021/3/9.
//

import UIKit

class CircleView: UIView {
    
    private var inProgress : CGFloat = 0
    var progress : CGFloat {
        get{
            return inProgress
        }
        set{
            inProgress = newValue
            
            if inProgress == 0 {
                let att : NSMutableAttributedString = NSMutableAttributedString.init()
                let dict1 = [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 20),
                             NSAttributedString.Key.foregroundColor: UIColor(hex: "151515")]
                att.append(NSAttributedString.init(string: "0%", attributes:dict1 ))
            
                cLabel.attributedText = att
            }else{
                let att : NSMutableAttributedString = NSMutableAttributedString.init()
                let dict1 = [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 20),
                             NSAttributedString.Key.foregroundColor: UIColor(hex: "151515")]
                att.append(NSAttributedString.init(string: "\(inProgress)", attributes: dict1))
                
                let dict2 = [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 15),
                             NSAttributedString.Key.foregroundColor: UIColor(hex: "151515")]
                att.append(NSAttributedString.init(string: "%", attributes: dict2))
                
                cLabel.attributedText = att
            }
            self.setNeedsDisplay()
        }
    }

    lazy var cLabel : UILabel  = {
        let label = UILabel.init()
        label.font = UIFont.boldSystemFont(ofSize: 20)
        label.textColor  = UIColor(hex: "151515")
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    
    override init(frame: CGRect) {
         super.init(frame: frame)
        self.backgroundColor = .clear
        self.addSubview(self.cLabel)
        self.cLabel.frame = self.bounds
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        
        let path : UIBezierPath = UIBezierPath.init()
        path.lineWidth = 3
        UIColor(hex: "ED7143").set()
        path.lineCapStyle = .round
        path.lineJoinStyle = .round
        let radius : CGFloat = (min(rect.size.width, rect.size.height) - 3*2) * 0.5
        let centerP : CGPoint = CGPoint(x: rect.size.width * 0.5, y: rect.size.height * 0.5)
//        path.addArc(withCenter: centerP, radius: radius, startAngle: (CGFloat)(M_PI * 1.5), endAngle: (CGFloat)(M_PI * 2 * inProgress), clockwise: true)
        let starA : CGFloat =  CGFloat( Double.pi * 1.5 )
        let endA  : CGFloat =   CGFloat( Double.pi * 2 ) * inProgress 
        path.addArc(withCenter: centerP, radius: radius, startAngle: starA, endAngle: endA, clockwise: true)
        path.stroke()
        
        
        
    }
    
    
}
